
var MapJS = {
map:'',
markersArray:[],
infoWindow: '',
latlng: '',
bounds:undefined,
initializeMap: function(extraJS) {
	  if($('#map_canvas').height(Math.max($(window).height()-180,440)).css('display')=='none')
	      $('#map_canvas').fadeIn(function(){
      	  latlng = new google.maps.LatLng(30.266,  -97.743);
      	  map = new google.maps.Map(document.getElementById("map_canvas"),{ zoom: 13,center: latlng,mapTypeId: google.maps.MapTypeId.ROADMAP});
      	  infoWindow = new google.maps.InfoWindow();
      	  bounds = new google.maps.LatLngBounds();
      	 this.markersArray=new Array();
      	 if(extraJS)
      		 google.maps.event.addDomListener(window, 'load', $('#workspace').append(extraJS));
	  });
	  
  },
  
applyMapResults: function(mapresults) {
	  this.deleteOverlays();
	  
	$(mapresults).each(function() {
		  var latlng = new google.maps.LatLng(this.lat,  this.lng);
		  MapJS.addMarker(this.zillowID,latlng);
		  bounds.extend(latlng);
	});
	map.fitBounds(bounds);

  },
  
  addMarker: function(id,location) {
	  var marker = new google.maps.Marker({
		  map:map,
	      position: location,
	      animation:google.maps.Animation.DROP
	  });
	  this.markersArray.push({id:id,marker:marker});
  },
  
//Deletes all markers in the array by removing references to them
  deleteOverlays: function() {
    if (this.markersArray) {
      for (i in this.markersArray) {
    	  this.markersArray[i].marker.setMap(null);
      }
      this.markersArray.length = 0;
    }
  },
  getInfoWindow:function() {
	  return infoWindow;
  },
  getMap:function() {
	  return map;
  },
  getLatLng:function() {
	  return latlng;  
  },
  reset:function() {
	 if(this.markersArray.length>0)
	  	this.getMap().fitBounds(bounds);
  }
  
};